﻿using System;
using System.Collections.Generic;

using System.Text;
using Xeno.Core;
using Xeno.Module;
using OpenTK;

using OpenTK.Graphics.OpenGL;
using OpenTK;

namespace Xeno.Physics
{
    public class XenoBoxCollider : XenoCollider
    {

    
        public XenoBoxCollider(string name,XenoNode attached,bool autoFit) :base(name,attached)
        {

            
            List<XenoNode> meshes = attached.AllWithType(typeof(XenoMesh), null);

            XenoMesh mesh = meshes[0].Mesh;


            Vector3[] bb = attached.BoundingBox;
            if (bb[0].Y == bb[1].Y)
            {
                bb[1].Y += 0.1f;
            }
            
            List<XenoNode> managers = Attached.GraphOwner.RootNode.AllWithType(typeof(XenoPhysicsManager), null);
            XenoNode man = managers[0];

            BoundingBox = bb;
            
            float ox = (bb[0].X+((bb[1].X-bb[0].X)/2.0f));
            float oy = (bb[0].Y+((bb[1].Y-bb[0].Y)/2.0f));
            float oz = (bb[0].Z+((bb[1].Z-bb[0].Z)/2.0f));
           

          
            SP = XenoPhysicsManager.Instance.PS.Factory.ShapePrimitiveManager.Create(attached.Name + "_BoxShadePrim");
            SP.CreateBox((bb[1].X - bb[0].X)/2.0f, (bb[1].Y - bb[0].Y)/2.0f,( bb[1].Z - bb[0].Z)/2.0f);

            S = XenoPhysicsManager.Instance.PS.Factory.ShapeManager.Create(attached.Name);
            S.Set(SP, Matrix4.Identity, 0.0f);
            S.SetShapePrimitiveTransformMatrix(Matrix4.CreateTranslation(ox, oy, oz));


             
         
        }
    }
}
